package com.gkh.sqlparser;

public class Str {

	// Skip spaces, new lines and tabs
	static char[] SkipSpaces(char[] input) {
		if (input == null)
			return null;
		char[] targetChars = { ' ', '\r', '\n', '\t' };
		String result = getLastStringFrom(String.valueOf(input), targetChars);
		return result.toCharArray();
	};

	// Skip comments, spaces, new lines and tabs
	static char[] SkipComments(char[] input) {
		if (input == null)
			return null;

		char[] tmp = SkipSpaces(input);
		String tmpStr = String.valueOf(tmp);
		if (tmpStr.startsWith("--") || tmpStr.toString().startsWith("//")) {
			int i = 0;
			for (; i < tmp.length; i++) {
				if (tmp[i] == '\n')
					break;
			}
			tmpStr = String.valueOf(tmp).substring(++i);
		} else {
			tmpStr = String.valueOf(tmp);
		}

		return tmpStr.toCharArray();
	}

	// Trim trailing spaces
	static String TrimTrailingSpaces(String input) {
		// Get the string length
		if (null == input)
			return null;
		char[] targetChars = { ' ' };
		String result = getBeforeStringFrom(input, targetChars);
		return result;
	}

	// Trim trailing spaces
	static char[] TrimTrailingSpaces(char[] input) {
		String tmpStr = TrimTrailingSpaces(String.valueOf(input));
		return tmpStr.toCharArray();
	}

	static String[] GetNextInList(char[] input) {
		if (input == null)
			return null;
		char[] tmp = SkipSpaces(input);
		char[] targetChars = { ',', ';' };
		String tmpStr = getLastStringFrom(String.valueOf(tmp), targetChars);
		char[] tmp2 = SkipSpaces(tmpStr.toCharArray());
		String tmpStr2 = String.valueOf(tmp2);
		System.out.println(tmpStr2);
		String[] tmpResult = tmpStr2.split(",|;|\\)", 2);
		String[] result = new String[tmpResult.length];
		for (int i = 0; i < result.length; i++) {
			result[i] = TrimTrailingSpaces(tmpResult[i]);
		}
		return result;
	}

	static String SkipUntil(char[] input,char ch) {
		if(input == null)
			return null;
		return getLastStringUntil(String.valueOf(input), ch);
	}
	
	static String GetSubtringUntil(char[] input,char ch) {
		if(input == null)
			return null;
		return getBeforeStringUntil(String.valueOf(input), ch);
	}
	
	static String ReplaceChar(char[] input,char what,char with) {
		if(input == null)
			return null;
		String inputStr = String.valueOf(input);
		char oldChar = what;
		char newChar = with;
		return inputStr.replace(oldChar, newChar);
	}
	
	static String IntToString(int int_value) {
		return String.valueOf(int_value);
	}
	
	/**
	 * Replace the first occurrence of a substring
	 * @param input
	 * @param what
	 * @param with
	 * @return
	 */
	static String ReplaceFirst(String input,String what,String with) {
		if (input == null)
			return null;
		return input.replaceFirst(what, with);
	}
	
	
	/**
	 * Convert size in bytes to string with MB,GB
	 * @param bytes
	 * @return
	 */
	static String FormatByteSize(double bytes) {
		if(bytes <= 1024)
			return String.format("%.2f", bytes) + " bytes";
		else
			if(bytes > 1024 && bytes <=1024 * 1024)
				return String.format("%.2f", bytes/1024.0) + " KB";
			else 
				if(bytes > 1024 * 1024 && bytes <= 1024*1024*1024)
					return String.format("%.2f", bytes/1024.0/1024.0) + " MB";
				else
					return String.format("%.2f", bytes/1024.0/1024.0/1024.0) + " GB";
	}
	
	/**
	 * Convert time in milliseconds to string with ms,sec
	 * @param time_ms
	 * @return
	 */
	static String FormatTime(int time_ms) {
		if(time_ms < 1000) 
			return String.format("%d ms", time_ms);
		else 
			if(time_ms < 1000 *60)
				return String.format("%.1f sec", ((double)time_ms)/1000);
			else
				return String.format("%d min %d sec", time_ms/60/1000,(time_ms%(60*1000))/1000);
		
	}
	
	/**
	 * Convert 2 digit date time part(century,2 digit year,month,day,hour,minute,second) t0 2 chars
	 * @param dt
	 * @return
	 */
//	static String Dt2Ch(int dt) {
//		char f = (char) ((char)dt/10);
//		char s = (char)dt%10;
//	}
	
	/**
	 * 依据soruce str和target chars，取不包含这个char的后面的str
	 */
	static String getLastStringFrom(String source, char... input) {
		if (source == null || input == null)
			return null;
		String result;
		int i = 0;
		for (; i < source.length(); i++) {
			char cur = source.charAt(i);
			int j = 0;
			for (; j < input.length; j++) {
				if (cur == input[j])
					break;
			}
			if (j == input.length)
				break;
		}
		result = source.substring(i);
		return result;
	}
	
	
	/**
	 * 依据soruce str和target chars，取chars出现之后的str
	 */
	static String getLastStringUntil(String source, char... input) {
		if (source == null || input == null)
			return null;
		String result;
		int i = 0;
		int flag = 0;
		for (; i < source.length(); i++) {
			char cur = source.charAt(i);
			int j = 0;
			for (; j < input.length; j++) {
				if (cur == input[j])
					flag = 1;
					break;
			}
			if (flag == 1)
				break;
		}
		result = source.substring(i);
		return result;
	}
	
	/**
	 * 依据soruce str和target chars，取chars出现之前的str
	 */
	static String getBeforeStringUntil(String source, char... input) {
		if (source == null || input == null)
			return null;
		String result;
		int i = 0;
		int flag = 0;
		for (; i < source.length(); i++) {
			char cur = source.charAt(i);
			int j = 0;
			for (; j < input.length; j++) {
				if (cur == input[j])
					flag = 1;
					break;
			}
			if (flag == 1)
				break;
		}
		result = source.substring(0,i);
		return result;
	}
	

	/**
	 * 依据str和char,取不包含这个char的前面的str
	 */
	static String getBeforeStringFrom(String source, char... input) {
		if (source == null || input == null)
			return null;
		String result;
		int i = source.length() - 1;
		int flag = 0;
		for (; i >= 0; i--) {
			char cur = source.charAt(i);
			int j = 0;
			for (; j < input.length; j++) {
				if (cur == input[j])
					break;
			}
			if (j == input.length)
				break;
		}
		result = source.substring(0, i + 1);
		return result;
	}

}
